<?php

class Exam_Model_Exam extends Core_Model_Item_Abstract
{
  protected $_parent_type = 'user';

  protected $_owner_type = 'user';

  protected $_parent_is_owner = true;
  
  protected $_mostUser = null;

  public function getHref($params = array())
  {
    $params = array_merge(array(
      'route' => 'exam_specific',
      'reset' => true,
      'exam_id' => $this->getIdentity(),
    ), $params);
    $route = $params['route'];
    $reset = $params['reset'];
    unset($params['route']);
    unset($params['reset']);
    return Zend_Controller_Front::getInstance()->getRouter()
      ->assemble($params, $route, $reset);
  }

  public function getPhotoUrl($type = null)
  {
    $file = Engine_Api::_()->getItemTable('storage_file')->getFile($this->photo_id, $type);
    if( !$file ) {
      return;
    }

    return $file->map();
  }
public function setPhoto($photo)
  {
    if( $photo instanceof Zend_Form_Element_File ) {
      $file = $photo->getFileName();
    } else if( is_array($photo) && !empty($photo['tmp_name']) ) {
      $file = $photo['tmp_name'];
    } else if( is_string($photo) && file_exists($photo) ) {
      $file = $photo;
    } else {
      throw new Exam_Model_Exception('invalid argument passed to setPhoto');
    }

    $name = basename($file);
    $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
    $params = array(
     'parent_id' => $this->getIdentity(),
     'parent_type'=>'event'
    );
    
    // Save
    $storage = Engine_Api::_()->storage();
    
    // Resize image (main)
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(720, 720)
      ->write($path.'/m_'.$name)
      ->destroy();

    // Resize image (profile)
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(200, 400)
      ->write($path.'/p_'.$name)
      ->destroy();

    // Resize image (normal)
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(140, 160)
      ->write($path.'/in_'.$name)
      ->destroy();

    // Resize image (icon)
    $image = Engine_Image::factory();
    $image->open($file);

    $size = min($image->height, $image->width);
    $x = ($image->width - $size) / 2;
    $y = ($image->height - $size) / 2;

    $image->resample($x, $y, $size, $size, 48, 48)
      ->write($path.'/is_'.$name)
      ->destroy();

    // Store
    $iMain = $storage->create($path.'/m_'.$name, $params);
    $iProfile = $storage->create($path.'/p_'.$name, $params);
    $iIconNormal = $storage->create($path.'/in_'.$name, $params);
    $iSquare = $storage->create($path.'/is_'.$name, $params);

    $iMain->bridge($iProfile, 'thumb.profile');
    $iMain->bridge($iIconNormal, 'thumb.normal');
    $iMain->bridge($iSquare, 'thumb.icon');

    // Remove temp files
    @unlink($path.'/p_'.$name);
    @unlink($path.'/m_'.$name);
    @unlink($path.'/in_'.$name);
    @unlink($path.'/is_'.$name);

    // Update row
    $this->modified_date = date('Y-m-d H:i:s');
    $this->photo_id = $iMain->file_id;
    $this->save();

    return $this;
  }
  
  public function getDescription()
  {
    // @todo decide how we want to handle multibyte string functions
    //$tmpBody = strip_tags($this->description);
    $tmpBody = $this->description;
    return ( Engine_String::strlen($tmpBody) > 255 ? Engine_String::substr($tmpBody, 0, 255) . '...' : $tmpBody );
  }
  
  public function categoryName()
  {
    $categoryTable = Engine_Api::_()->getDbtable('categories', 'exam');
    return $categoryTable->select()
        ->from($categoryTable, 'category_name')
        ->where('category_id = ?', $this->category_id)
        ->limit(1)
        ->query()
        ->fetchColumn();
  }
  
  public function count()
  {
    $photoTable = Engine_Api::_()->getItemTable('exam_examquestion');
    return $photoTable->select()
        ->from($photoTable, new Zend_Db_Expr('COUNT(question_id)'))
        ->where('exam_id = ?', $this->exam_id)
        ->limit(1)
        ->query()
        ->fetchColumn();
  }
  
  /**
   * Gets a proxy object for the comment handler
   *
   * @return Engine_ProxyObject
   **/
  public function comments()
  {
    return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('comments', 'core'));
  }


  /**
   * Gets a proxy object for the like handler
   *
   * @return Engine_ProxyObject
   **/
  public function likes()
  {
    return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('likes', 'core'));
  }
  
  protected function _postDelete()
  {
    //to do
    parent::_postDelete();
  }
  
  public function isAllow($user_id){
    $testTable = Engine_Api::_()->getDbtable('tests','exam');
    $hadtest =  $testTable->userHadTest($user_id,$this->exam_id);
    if(!$this->attempts_allowed && $hadtest) return false;
    return true;
  }
  
  public function getMostUser(){
    if($this->most_user_id == 0) return null;
    if(is_null($this->_mostUser)) {
        $this->_mostUser = Engine_Api::_()->getItem('user',$this->most_user_id);
    }
    return $this->_mostUser;
  }
}
